using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Soopah.Xna.Input;



namespace Unibot
{
    public static class MyGamePad
    {
        private static MyGamePadState.Type CONTROL_METHOD = MyGamePadState.Type.Keyboard;

        public static MyGamePadState GetState(PlayerIndex player)
        {
            int playerIndex = System.Convert.ToInt16(player);

            try
            {
                MyGamePadState myGamePadState = new MyGamePadState();

                if (CONTROL_METHOD == MyGamePadState.Type.PS2)
                {
                    myGamePadState.Buttons.Triangle = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[0];
                    myGamePadState.Buttons.Circle = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[1];
                    myGamePadState.Buttons.Cross = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[2];
                    myGamePadState.Buttons.Square = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[3];
                    myGamePadState.Buttons.L2 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[4];
                    myGamePadState.Buttons.R2 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[5];
                    myGamePadState.Buttons.L1 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[6];
                    myGamePadState.Buttons.R1 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[7];
                    myGamePadState.Buttons.Select = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[8];
                    myGamePadState.Buttons.Start = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[9];
                    myGamePadState.Buttons.L3 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[10];
                    myGamePadState.Buttons.R3 = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].Buttons.List[11];

                    myGamePadState.DPad.Down = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].DPad.Down;
                    myGamePadState.DPad.Up = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].DPad.Up;
                    myGamePadState.DPad.Left = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].DPad.Left;
                    myGamePadState.DPad.Right = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].DPad.Right;

                    myGamePadState.Thumbsticks.Left = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].ThumbSticks.Left;
                    myGamePadState.Thumbsticks.Right = Soopah.Xna.Input.DirectInputGamepad.Gamepads[playerIndex].ThumbSticks.Right;                    
                }
                else if (CONTROL_METHOD == MyGamePadState.Type.Keyboard)
                {
                    KeyboardState kb = Keyboard.GetState();
                    myGamePadState.Buttons.Triangle = createButtonState(kb.IsKeyDown(Keys.I));
                    myGamePadState.Buttons.Circle = createButtonState(kb.IsKeyDown(Keys.L));
                    myGamePadState.Buttons.Cross = createButtonState(kb.IsKeyDown(Keys.K));
                    myGamePadState.Buttons.Square = createButtonState(kb.IsKeyDown(Keys.J));
                    myGamePadState.Buttons.L2 = createButtonState(kb.IsKeyDown(Keys.R)); // does not need to exist yet... need a good keyboard mapping
                    myGamePadState.Buttons.R2 = ButtonState.Released; // so it's ok to say that they aren't being pressed 
                    myGamePadState.Buttons.L1 = ButtonState.Released;
                    myGamePadState.Buttons.R1 = ButtonState.Released;
                    myGamePadState.Buttons.Select = createButtonState(kb.IsKeyDown(Keys.Escape));
                    myGamePadState.Buttons.Start = createButtonState(kb.IsKeyDown(Keys.Enter));
                    myGamePadState.Buttons.L3 = ButtonState.Released;
                    myGamePadState.Buttons.R3 = ButtonState.Released;

                    myGamePadState.DPad.Down = createButtonState(kb.IsKeyDown(Keys.S));
                    myGamePadState.DPad.Up = createButtonState(kb.IsKeyDown(Keys.W));
                    myGamePadState.DPad.Left = createButtonState(kb.IsKeyDown(Keys.A));
                    myGamePadState.DPad.Right = createButtonState(kb.IsKeyDown(Keys.D));

                    myGamePadState.Thumbsticks.Left = new Vector2();
                    myGamePadState.Thumbsticks.Right = new Vector2();
                }
                return myGamePadState;
            }
            catch (Exception e)
            {
                Console.Write("Unable to create a MyGamePadState object: " + e.Message);
                return new MyGamePadState();
            }
        }

        public static ButtonState createButtonState(bool pressed){
            if (pressed)
            {
                return ButtonState.Pressed;
            }
            else
            {
                return ButtonState.Released;
            }
        }
    }

}
